package com.atty.dms.tools;

import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@AllArgsConstructor
public class RedisUtil {


    private final StringRedisTemplate redisTemplate;

    private static final String INVALID_TOKEN_PREFIX = "invalid_token:";

    /**
     * 将 Token 标记为已注销
     *
     * @param token JWT Token
     * @param expiration 过期时间（秒）
     */
    public void markTokenAsInvalid(String token, Long expiration) {
        String key = INVALID_TOKEN_PREFIX + token;
        redisTemplate.opsForValue().set(key, "invalid", expiration, TimeUnit.SECONDS);
    }

    /**
     * 检查 Token 是否已注销
     *
     * @param token JWT Token
     * @return true 表示已注销，false 表示未注销
     */
    public boolean isTokenInvalid(String token) {
        String key = INVALID_TOKEN_PREFIX + token;
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }
}